import api from "./api.js"
import config from "./config.js"

export default {
	isbool(param) {
		// this.log(param)
		if (param === '') {
			return false
		}
		if (param === []) {
			return false
		}
		if (param === "[]") {
			return false
		}
		if (param === 'undefined') {
			return false
		}
		if (param === undefined) {
			return false
		}
		if (param === {}) {
			return false
		}
		if (param === 0) {
			return false
		}
		if (param === "0") {
			return false
		}
		if (param === "null") {
			return false
		}
		if (param === null) {
			return false
		}
		return true
	},
	isTrue(param) {
		return this.isbool(param)
	},
	log(info, text) {
		if (text !== undefined) {
			info = text + info
		}
		if (config.loggerStatus == 1) {
			this.log(info);
		}
		if (config.loggerStatus == 2) {
			logHtml(
				"==========================================" + "<br>" + "caller:" + log.caller + "<br>" +
				"--------------------------------------------------------------------------" + "<br>" +
				info + "<br>" +
				"==========================================" + "<br><" + this.caller + "br><br><br>"
				// + "<br><br><br><br>"
			);
		}
	},
	logJson(info, text) {
		info = JSON.stringify(info)
		this.log(info, text)
	},
	getDataFromUrl(url) {
		var queryArr = data.split("?")
		return queryArr[1]
	},
	getDataFromQuery(data) {
		var result = {}
		var queryArr = data.split("&")
		queryArr.forEach(function(item) {
			var value = item.split("=")[1]
			var key = item.split("=")[0]
			result[key] = value
		})
		return result
	},
	getLocData(key) {
		return uni.getStorageSync(key)
	},
	getLocDataObject(key) {
		let string = this.getLocData(key)
		if(this.isbool(string)){
			try{
				return JSON.parse(string)
			}catch(e){
				return false
			}
			
		}
		return false
	},
	setLocData(key, value) {
		if (typeof value != "string") {
			value = JSON.stringify(value)
		}
		uni.setStorageSync(key, value)
	},
	getLocDataAsync(key, func) {
		uni.getStorage({
			key: key,
			success: function(res) {
				func(res.data)
			}
		});
	},
	setLocDataAsync(key, value) {
		if (typeof value != "string") {
			value = JSON.stringify(value)
		}
		uni.setStorage({
			key: key,
			data: value,
			success: function() {
				this.log('success');
			}
		});
		// uni.setStorageSync(key, value)
	},
	delLocData(key) {
		uni.removeStorageSync(key);
	},
	clearLocData(key) {
		uni.clearStorageSync();
	},


	objDelIndex(obj, index) {
		var tmp = {};
		for (var i in obj) {
			if (i != index) {
				tmp[i] = obj[i]
			}
		}
		return tmp;
	},

	objDelValue(obj, value) {
		var tmp = {};
		for (var i in obj) {
			if (obj[i] != value) {
				tmp[i] = obj[i]
			}
		}
		return tmp;
	},


	arrayDelIndex(array, index) {
		var tmp = [];
		array.map(function(data, i) {
			if (i != index) {
				tmp.push(data)
			}
		})
		return tmp;
	},

	arrayDelValue(array, value) {
		var tmp = [];
		array.map(function(data, i) {
			if (data != value) {
				tmp.push(data)
			}
		})
		return tmp;
	},

	isInArray(val, array) {
		if (!this.isTrue(array)) {
			return false
		}
		for (var i in array) {
			if (array[i] == val) {
				return true;
			}
		}
		return false;
	},
	arrayReplaceValue(array, find, replace) {
		var tmp = [];
		array.map(function(data, i) {
			if (data != find) {
				tmp.push(data)
			} else {
				tmp.push(replace)
			}
		})
		return tmp;
	},
	showToast(title, duration) {
		if (duration == undefined) {
			duration = 2000
		}
		uni.showToast({
			icon:"none",
			title: title,
			duration: duration
		});
	},
	picUrl(pic) {
		if (this.isbool(pic)) {
			return api.getAssetBaseUrl() + pic;
		}
		return ""
	},
	picBg(pic) {
		var rnd = Math.floor(Math.random() * 10); //0-9
		var color = ["#ffd6d6", "#ffead6", "#fffed6", "#eeffd6", "#ddffd6", "#d6ffef", "#d6f1ff", "#dbd6ff", "#f8d6ff",
			"#ffd6e3"
		]
		if (this.isbool(pic)) {
			return "background-size: cover;background-color: " + color[rnd] + ";background-image: url('" + this.picUrl(pic) +
				"');";
		} else {
			return "background-size: cover;background-color: " + color[rnd] + ";";
		}
	},
	rndbg() {
		var rnd = Math.floor(Math.random() * 10); //0-9
		return ""
	},
	isLogin() {
		return this.isbool(this.getLocData('login'))
	},
	str2time(str) {
		if (!this.isTrue(str)) return '-'
		var date = new Date(str);
		return this.getDateTime(date)
	},

	getDate(date) {
		if (date == undefined) {
			date = new Date()
		}
		return date.getFullYear() + "-" + ((date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1)) +
			"-" + date.getDate();
	},

	getTime(date) {
		if (date == undefined) {
			date = new Date()
		}
		return (date.getHours()<10?'0'+ date.getHours(): date.getHours()) +
			":" + (date.getMinutes()<10?'0'+ date.getMinutes(): date.getMinutes()) +
			":" +  (date.getSeconds()<10?'0'+ date.getSeconds(): date.getSeconds()) ;
	},
	getTimestamp(date) {
		return parseInt(this.getTimestampMs(date) / 1000);
	},
	getTimestampMs(date) {
		if (date == undefined) {
			date = new Date()
		}
		return date.getTime();
	},

	getDateTime(date) {
		if (date === undefined) {
			date = new Date()
		}
		return date.getFullYear() + "-" +
			((date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1)) +
			"-" + date.getDate() +
			" " + (date.getHours()<10?'0'+ date.getHours(): date.getHours())  +
			":" + (date.getMinutes()<10?'0'+ date.getMinutes(): date.getMinutes()) +
			":" + (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
	},
	timeLeft(str) {
		var date = new Date();
		var now = date.getTime();
		//设置截止时间
		//     var str = "2019/10/1 00:00:00";
		var endDate = new Date(str);
		var end = endDate.getTime();
		//时间差
		var leftTime = end - now;
		//定义变量 d,h,m,s保存倒计时的时间
		var d, h, m, s;
		d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
		h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
		m = Math.floor(leftTime / 1000 / 60 % 60);
		s = Math.floor(leftTime / 1000 % 60);
		
		if (leftTime > 1) {
			let re = '';
			if (d > 0){
				re = re + d + "天"
			}
			if (h > 0){
				re = re + h + "小时"
			}
			if (m > 0){
				re = re + m + "分钟"
			}
			// if (s > 0){
			// 	re = re + s + "秒"
			// }
			return re
			// return d + "天" +
			// 	h + "时" +
			// 	m + "分";
			// + s + "秒";
		} else {
			//倒计时结束
			return "已过期";
		}
	},

	timePass(str) {
		var date = new Date();
		var now = date.getTime();
		//设置截止时间
		//     var str = "2019/10/1 00:00:00";
		var endDate = new Date(str);
		var end = endDate.getTime();
		//时间差
		//     var leftTime = end - now;
		var leftTime = now - end;
		//定义变量 d,h,m,s保存倒计时的时间
		var d, h, m, s;
		d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
		h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
		m = Math.floor(leftTime / 1000 / 60 % 60);
		s = Math.floor(leftTime / 1000 % 60);

		var showStr = "";
		// if (s > 0) { showStr = s + "秒" + showStr; }
		// if (m > 0) { showStr = m + "分" + showStr; }
		// if (h > 0) { showStr = h + "时" + showStr; }
		// if (d > 0) { showStr = d + "天" + showStr; }
		// return  showStr;
		if (s > 0) {
			showStr = s + "秒";
		}
		if (m > 0) {
			showStr = m + "分钟";
		}
		if (h > 0) {
			showStr = h + "小时";
		}
		if (d > 0) {
			showStr = d + "天";
		}
		return showStr;
	},

	random_string() {
		var len = 32;
		var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
		var maxPos = chars.length;
		var pwd = '';
		for (var i = 0; i < len; i++) {
			pwd += chars.charAt(Math.floor(Math.random() * maxPos));
		}
		return pwd;
	},

	get_suffix(filename) {
		pos = filename.lastIndexOf('.')
		suffix = ''
		if (pos != -1) {
			suffix = filename.substring(pos)
		}
		return suffix;
	},

	checkIsPhone(val) {
		var myreg = /^[1][3,4,5,6,7,8][0-9]{9}$/;

		if (!myreg.test(val)) {
			return false;
		} else {
			return true;
		}
	},
	iriId(iri){
		return iri.split("/").pop()
	}

}
